package com.ui.manager
{
	import com.ui.containers.Panel;
	import com.ui.containers.TitleWindow;
	import com.ui.core.UIComponent;

	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	/**
	 * @author Cafe
	 */
	public class ViewManage
	{
		public static var viewList : Vector.<UIComponent> = new Vector.<UIComponent>();
		public static var topPanel : UIComponent;

		public static function add( value : UIComponent ) : void
		{
			if (value is Panel || value is TitleWindow && viewList.indexOf( value ) == -1)
			{
				viewList.push( value );
				value.addEventListener( MouseEvent.MOUSE_DOWN, viewClickHandler );
				topPanel = value;
			}
		}

		private static function viewClickHandler( event : MouseEvent ) : void
		{
			var index : int = UIManager.root.numChildren - 1;
			if (index < 0) return;
			var clickObj : * = event.currentTarget;
			var ui : DisplayObject = UIManager.root.getChildAt( index );
			if (ui && ui.parent == clickObj.parent)
				UIManager.root.swapChildren( ui, clickObj );
		}

		public static function remove( value : UIComponent ) : void
		{
			var index : int = viewList.indexOf( value );
			if (index == -1) return;
			viewList[index].removeEventListener( MouseEvent.CLICK, viewClickHandler );
			viewList.splice( index, 1 );
		}

		public static function removeTopUI() : Boolean
		{
			if (viewList.length == 0) return false;
			viewList[viewList.length - 1].hide();
			return true;
		}

		public static function topView( value : * ) : void
		{
		}
	}
}